package com.bykj.property.config.shiro;

import com.alibaba.fastjson.JSON;
import com.bykj.property.config.common.exception.CustomException;
import com.bykj.property.controller.vo.response.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;


/**
 * @author yiming
 * @title: CustomSessionManager
 * @projectName property
 * @description: TODO
 * @date 2019/6/2616:16
 */
@Slf4j
public class CustomSessionManager extends DefaultWebSessionManager{
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
        String token = httpServletRequest.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "token");
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, token);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
            return token;

        } else {
            return super.getSessionId(request, response);
        }

    }

}
